package com.gitee.feizns.itrip.personal.auth.controller;

import com.gitee.feizns.itrip.personal.auth.entity.ao.ForAcceptLogin;
import com.gitee.feizns.itrip.personal.auth.service.LoginService;
import com.gitee.feizns.itrip.personal.beans.dto.Dto;
import com.gitee.feizns.itrip.personal.beans.dto.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author feizns
 * @since 2018/12/28 0028
 */
@RestController
@RequestMapping("/api")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/dologin")
    public Dto<Token> doLogin(@Validated ForAcceptLogin acceptLogin) {
        return Dto.getSuccessDto("登录成功.", loginService.login(acceptLogin.getName(), acceptLogin.getPassword()));
    }

    @GetMapping(value = "/logout", headers = "token")
    public Dto logout(@RequestHeader("token") String token) {
        loginService.logout(token);
        return Dto.getSuccessDto("注销成功.");
    }

    @PostMapping(value = "/retoken", headers = "token")
    public Dto<Token> reToken(@RequestHeader("token") String token) {
        return Dto.getSuccessDto(null, loginService.reToken(token));
    }
    
}
